//
//  GlobalMethod.m
//  Comma
//
//  Created by liuchang on 15/1/17.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//
#import "SSKeychain.h"
#import "GlobalMethod.h"
#import <CommonCrypto/CommonDigest.h>
@implementation GlobalMethod

+ (void)drawLineWithStartPoint:(CGPoint)startPoint EndPoint:(CGPoint)endPoint inUIView:(UIView *)view
{
    UIImageView *imageView=[[UIImageView alloc] initWithFrame:view.frame];
    [view addSubview:imageView];
    UIGraphicsBeginImageContext(imageView.frame.size);
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 0.5f);  //线宽
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), NO);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 200 / 255.0f, 199 / 255.0f, 204 / 255.0f, 0.7);  //颜色
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), startPoint.x, startPoint.y);  //起点坐标
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), endPoint.x, endPoint.y);   //终点坐标
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    imageView.image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}


+ (void)drawLineWithStartPoint:(CGPoint)startPoint EndPoint:(CGPoint)endPoint inUIView:(UIView *)view myTag:(NSInteger)myTag
{
    UIImageView *imageView=[[UIImageView alloc] initWithFrame:view.frame];
    imageView.tag = myTag;
    [view addSubview:imageView];
    UIGraphicsBeginImageContext(imageView.frame.size);
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 0.5f);  //线宽
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), NO);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 200 / 255.0f, 199 / 255.0f, 204 / 255.0f, 0.7);  //颜色
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), startPoint.x, startPoint.y);  //起点坐标
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), endPoint.x, endPoint.y);   //终点坐标
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    imageView.image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}



+ (NSString *)puberNickChangeString:(NSString *)puberNick
{
    if  ([puberNick rangeOfString:@"s_user"].location != NSNotFound && [puberNick rangeOfString:@"_idt"].location != NSNotFound) {
        NSString *puberNick = [NSString stringWithFormat:@" • 学生用户 • 认证用户"];
        return puberNick;
    } else if  ([puberNick rangeOfString:@"t_user"].location != NSNotFound && [puberNick rangeOfString:@"_idt"].location != NSNotFound) {
        NSString *puberNick = [NSString stringWithFormat:@" • 教师用户 • 认证用户"];
        return puberNick;
    } else if  ([puberNick rangeOfString:@"s_user"].location != NSNotFound && [puberNick rangeOfString:@"_idt"].location == NSNotFound) {
        NSString *puberNick = [NSString stringWithFormat:@" • 学生用户 • 未认证用户"];
        return puberNick;
    } else if  ([puberNick rangeOfString:@"t_user"].location != NSNotFound && [puberNick rangeOfString:@"_idt"].location == NSNotFound) {
        NSString *puberNick = [NSString stringWithFormat:@" • 教师用户 • 未认证用户"];
        return puberNick;
    } else
    {
        return @"";
    }
}

+ (NSString *)getFullURLWithURLString:(NSString *)urlStr URLDic:(NSDictionary *)parmaters
{
    if ([[parmaters allKeys] count] != 0) {
        urlStr = [NSString stringWithFormat:@"%@?", urlStr];
        for (NSString * key in parmaters) {
            urlStr = [NSString stringWithFormat:@"%@%@=%@&", urlStr, key, [parmaters objectForKey:key]];
        }
        urlStr = [urlStr substringToIndex:[urlStr length] - 1];
    }
    return urlStr;
}


//iOS 生成 UUID（或者叫GUID）例子代码

+ (NSString *)GetIOSUUID
{
    NSString * currentDeviceUUIDStr = [SSKeychain passwordForService:@"com.isuso"account:@"uuid"];
    if (currentDeviceUUIDStr == nil || [currentDeviceUUIDStr isEqualToString:@""])
    {
        NSUUID * currentDeviceUUID  = [UIDevice currentDevice].identifierForVendor;
        currentDeviceUUIDStr = currentDeviceUUID.UUIDString;
        currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
        currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString];
        [SSKeychain setPassword: currentDeviceUUIDStr forService:@"com.isuso"account:@"uuid"];
    }
    return currentDeviceUUIDStr;
}

+ (NSString *)getApproveKey
{
    NSString *ApproveKey = [SSKeychain passwordForService:@"com.isuso" account:@"appriveKey"];
    if (ApproveKey == nil || [ApproveKey isEqualToString:@""])
    {
        NSString * currentDeviceUUIDStr = [GlobalMethod GetIOSUUID];
        ApproveKey = [self md5:[NSString stringWithFormat:@"douhao%@", currentDeviceUUIDStr]];
        ApproveKey = [ApproveKey lowercaseString];
        [SSKeychain setPassword:ApproveKey forService:@"com.isuso" account:@"appriveKey"];
    }
    return ApproveKey;
}

+(NSString *) md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, strlen(cStr), result );
    NSString *string = [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]];
    return string;
}

+ (NSString *) phonetic:(NSString*)sourceString
{
    NSMutableString *source = [sourceString mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);
    return source;
}

+ (iPhoneNumber)getiPhoneNumber
{
    if (SCREEN_HEIGHT == 480) {
        return iPhone_4;
    }
    else if (SCREEN_HEIGHT == 568) {
        return iPhone_5;
    }
    else if (SCREEN_HEIGHT == 667 ) {
        return iPhone_6;
    }
    else if (SCREEN_HEIGHT == 736) {
        return iPhone_6Plus;
    }
    else {
        return unKnowType;
    }
}

// 特殊字符字典
+ (NSMutableDictionary *)specialCharacter
{   
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"Α" forKey:@"&Alpha;"];
    [dic setObject:@"Β" forKey:@"&Beta;"];
    [dic setObject:@"Γ" forKey:@"&Gamma;"];
    [dic setObject:@"Δ" forKey:@"&Delta;"];
    [dic setObject:@"Ε" forKey:@"&Epsilon;"];
    [dic setObject:@"Ζ" forKey:@"&Zeta;"];
    [dic setObject:@"Η" forKey:@"&Eta;"];
    [dic setObject:@"Θ" forKey:@"&Theta;"];
    [dic setObject:@"Ι" forKey:@"&Iota;"];
    [dic setObject:@"Κ" forKey:@"&Kappa;"];
    [dic setObject:@"Λ" forKey:@"&Lambda;"];
    [dic setObject:@"Μ" forKey:@"&Mu;"];
    [dic setObject:@"Ν" forKey:@"&Nu;"];
    [dic setObject:@"Ξ" forKey:@"&Xi;"];
    [dic setObject:@"Ο" forKey:@"&Omicron;"];
    [dic setObject:@"Π" forKey:@"&Pi;"];
    [dic setObject:@"Ρ" forKey:@"&Rho;"];
    [dic setObject:@"Σ" forKey:@"&Sigma;"];
    [dic setObject:@"Τ" forKey:@"&Tau;"];
    [dic setObject:@"Υ" forKey:@"&Upsilon;"];
    [dic setObject:@"Φ" forKey:@"&Phi;"];
    [dic setObject:@"Χ" forKey:@"&Chi;"];
    [dic setObject:@"Ψ" forKey:@"&Psi;"];
    [dic setObject:@"Ω" forKey:@"&Omega;"];
    [dic setObject:@"α" forKey:@"&alpha;"];
    [dic setObject:@"β" forKey:@"&beta;"];
    [dic setObject:@"γ" forKey:@"&gamma;"];
    [dic setObject:@"δ" forKey:@"&delta;"];
    [dic setObject:@"ε" forKey:@"&epsilon;"];
    [dic setObject:@"ζ" forKey:@"&zeta;"];
    [dic setObject:@"η" forKey:@"&eta;"];
    [dic setObject:@"θ" forKey:@"&theta;"];
    [dic setObject:@"ι" forKey:@"&iota;"];
    [dic setObject:@"κ" forKey:@"&kappa;"];
    [dic setObject:@"λ" forKey:@"&lambda;"];
    [dic setObject:@"μ" forKey:@"&mu;"];
    [dic setObject:@"ν" forKey:@"&nu;"];
    [dic setObject:@"ξ" forKey:@"&xi;"];
    [dic setObject:@"ο" forKey:@"&omicron;"];
    [dic setObject:@"π" forKey:@"&pi;"];
    [dic setObject:@"ρ" forKey:@"&rho;"];
    [dic setObject:@"ς" forKey:@"&sigmaf;"];
    [dic setObject:@"σ" forKey:@"&sigma;"];
    [dic setObject:@"τ" forKey:@"&tau;"];
    [dic setObject:@"υ" forKey:@"&upsilon;"];
    [dic setObject:@"φ" forKey:@"&phi;"];
    [dic setObject:@"χ" forKey:@"&chi;"];
    [dic setObject:@"ψ" forKey:@"&psi;"];
    [dic setObject:@"ω" forKey:@"&omega;"];
    [dic setObject:@"ϑ" forKey:@"&thetasym;"];
    [dic setObject:@"ϒ" forKey:@"&upsih;"];
    [dic setObject:@"ϖ" forKey:@"&piv;"];
    [dic setObject:@"•" forKey:@"&bull;"];
    [dic setObject:@"…" forKey:@"&hellip;"];
    [dic setObject:@"′" forKey:@"&prime;"];
    [dic setObject:@"″" forKey:@"&Prime;"];
    [dic setObject:@"‾" forKey:@"&oline;"];
    [dic setObject:@"⁄" forKey:@"&frasl;"];
    [dic setObject:@"℘" forKey:@"&weierp;"];
    [dic setObject:@"ℑ" forKey:@"&image;"];
    [dic setObject:@"ℜ" forKey:@"&real;"];
    [dic setObject:@"™" forKey:@"&trade;"];
    [dic setObject:@"ℵ" forKey:@"&alefsym;"];
    [dic setObject:@"←" forKey:@"&larr;"];
    [dic setObject:@"↑" forKey:@"&uarr;"];
    [dic setObject:@"→" forKey:@"&rarr;"];
    [dic setObject:@"↓" forKey:@"&darr;"];
    [dic setObject:@"↔" forKey:@"&harr;"];
    [dic setObject:@"↵" forKey:@"&crarr;"];
    [dic setObject:@"⇐" forKey:@"&lArr;"];
    [dic setObject:@"⇑" forKey:@"&uArr;"];
    [dic setObject:@"⇒" forKey:@"&rArr;"];
    [dic setObject:@"⇓" forKey:@"&dArr;"];
    [dic setObject:@"⇔" forKey:@"&hArr;"];
    [dic setObject:@"∀" forKey:@"&forall;"];
    [dic setObject:@"∂" forKey:@"&part;"];
    [dic setObject:@"∃" forKey:@"&exist;"];
    [dic setObject:@"∅" forKey:@"&empty;"];
    [dic setObject:@"∇" forKey:@"&nabla;"];
    [dic setObject:@"∈" forKey:@"&isin;"];
    [dic setObject:@"∉" forKey:@"&notin;"];
    [dic setObject:@"∋" forKey:@"&ni;"];
    [dic setObject:@"∏" forKey:@"&prod;"];
    [dic setObject:@"∑" forKey:@"&sum;"];
    [dic setObject:@"−" forKey:@"&minus;"];
    [dic setObject:@"∗" forKey:@"&lowast;"];
    [dic setObject:@"√" forKey:@"&radic;"];
    [dic setObject:@"∝" forKey:@"&prop;"];
    [dic setObject:@"∞" forKey:@"&infin;"];
    [dic setObject:@"∠" forKey:@"&ang;"];
    [dic setObject:@"∧" forKey:@"&and;"];
    [dic setObject:@"∨" forKey:@"&or;"];
    [dic setObject:@"∩" forKey:@"&cap;"];
    [dic setObject:@"∪" forKey:@"&cup;"];
    [dic setObject:@"∫" forKey:@"&int;"];
    [dic setObject:@"∴" forKey:@"&there4;"];
    [dic setObject:@"∼" forKey:@"&sim;"];
    [dic setObject:@"≅" forKey:@"&cong;"];
    [dic setObject:@"≈" forKey:@"&asymp;"];
    [dic setObject:@"≠" forKey:@"&ne;"];
    [dic setObject:@"≡" forKey:@"&equiv;"];
    [dic setObject:@"≤" forKey:@"&le;"];
    [dic setObject:@"≥" forKey:@"&ge;"];
    [dic setObject:@"⊂" forKey:@"&sub;"];
    [dic setObject:@"⊃" forKey:@"&sup;"];
    [dic setObject:@"⊄" forKey:@"&nsub;"];
    [dic setObject:@"⊆" forKey:@"&sube;"];
    [dic setObject:@"⊇" forKey:@"&supe;"];
    [dic setObject:@"⊕" forKey:@"&oplus;"];
    [dic setObject:@"⊗" forKey:@"&otimes;"];
    [dic setObject:@"⊥" forKey:@"&perp;"];
    [dic setObject:@"⋅" forKey:@"&sdot;"];
    [dic setObject:@"⌈" forKey:@"&lceil;"];
    [dic setObject:@"⌉" forKey:@"&rceil;"];
    [dic setObject:@"⌊" forKey:@"&lfloor;"];
    [dic setObject:@"⌋" forKey:@"&rfloor;"];
    [dic setObject:@"◊" forKey:@"&loz;"];
    [dic setObject:@"♠" forKey:@"&spades;"];
    [dic setObject:@"♣" forKey:@"&clubs;"];
    [dic setObject:@"♥" forKey:@"&hearts;"];
    [dic setObject:@"♦" forKey:@"&diams;"];
    [dic setObject:@" " forKey:@"&nbsp;"];
    [dic setObject:@"¡" forKey:@"&iexcl;"];
    [dic setObject:@"¢" forKey:@"&cent;"];
    [dic setObject:@"£" forKey:@"&pound;"];
    [dic setObject:@"¤" forKey:@"&curren;"];
    [dic setObject:@"¥" forKey:@"&yen;"];
    [dic setObject:@"¦" forKey:@"&brvbar;"];
    [dic setObject:@"§" forKey:@"&sect;"];
    [dic setObject:@"¨" forKey:@"&uml;"];
    [dic setObject:@"©" forKey:@"&copy;"];
    [dic setObject:@"ª" forKey:@"&ordf;"];
    [dic setObject:@"«" forKey:@"&laquo;"];
    [dic setObject:@"¬" forKey:@"&not;"];
    [dic setObject:@"®" forKey:@"&reg;"];
    [dic setObject:@"¯" forKey:@"&macr;"];
    [dic setObject:@"°" forKey:@"&deg;"];
    [dic setObject:@"±" forKey:@"&plusmn;"];
    [dic setObject:@"²" forKey:@"&sup2;"];
    [dic setObject:@"³" forKey:@"&sup3;"];
    [dic setObject:@"´" forKey:@"&acute;"];
    [dic setObject:@"µ" forKey:@"&micro;"];
    [dic setObject:@"“" forKey:@"&ldquo;"];
    [dic setObject:@"”" forKey:@"&rdquo;"];
    [dic setObject:@"—" forKey:@"&mdash;"];
//    [dic setObject:@"<" forKey:@"&lt;"];
//    [dic setObject:@">" forKey:@"&gt;"];
    
    
    return dic;

}
#pragma mark - 将html特殊字符进行替换
+ (NSString *)replaceCharacterWithString:(NSString *)string
{
    NSDictionary *dic = [GlobalMethod specialCharacter];
    for (NSString *key in dic) {
        NSRange range = [string rangeOfString:key];
        if (range.length != 0) {
            NSString *value = [dic objectForKey:key];
            string = [string stringByReplacingOccurrencesOfString:key withString:value];
        }
    }
    return string;
}


@end
